package com.wtwd.campus.wx.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.DeviceFlag;
import com.wtwd.campus.model.dto.QueryAppDisAbleStateDto;
import com.wtwd.campus.model.dto.SetAppDisableDto;
import com.wtwd.campus.model.vo.AuthDeviceIsSupportAppVo;
import com.wtwd.campus.model.vo.QueryDeviceAppVo;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.service.DeviceAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mjy
 * @date 2022/12/09
 * @description
 */
@Api(tags = "微信设备控制app模块")
@RestController
@RequestMapping("/wx/device-app")
public class DeviceAppController {

    @Resource
    private DeviceAppService deviceAppService;


    private static final Logger logger = LoggerFactory.getLogger(DeviceAppController.class);

    /**
     * 获取设备app
     * 由于app数据量不多,且基本情况都是基本固定,此处暂不做懒加载(分页)
     *
     * @param imei
     * @return
     */
    @ApiOperation(value = "获取设备app", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imei", value = "设备imei号", required = true, dataType = "String")
    })
    @GetMapping("/queryDeviceApp")
    public ResultVO<List<QueryDeviceAppVo>> queryDeviceApp(@RequestParam("imei") String imei) {
        logger.info("queryDeviceApp deviceId={}", imei);
        AuthDeviceIsSupportAppVo authDeviceIsSupportAppVo = deviceAppService.authDeviceIsSupportApp(imei);
        if (authDeviceIsSupportAppVo.getRespCode().getCode() != RespCode.SUCCESS.getCode()) {
            return ResultVO.error(authDeviceIsSupportAppVo.getRespCode());
        }
        List<QueryDeviceAppVo> queryDeviceAppVos = deviceAppService.queryDeviceAppByDeviceId(authDeviceIsSupportAppVo.getDeviceInfo().getDeviceId());
        return ResultVO.ok(queryDeviceAppVos);
    }

    @ApiOperation(value = "查询设备开关", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imei", value = "设备imei号", required = true, dataType = "String")
    })
    @GetMapping("/queryDeviceSwitch")
    public ResultVO<DeviceFlag> queryDeviceSwitch(@RequestParam String imei) {
        logger.info("queryDeviceSwitch deviceId={}", imei);
        AuthDeviceIsSupportAppVo authDeviceIsSupportAppVo = deviceAppService.authDeviceIsSupportApp(imei);
        if (authDeviceIsSupportAppVo.getRespCode().getCode() != RespCode.SUCCESS.getCode()) {
            return ResultVO.error(authDeviceIsSupportAppVo.getRespCode());
        }
        DeviceFlag deviceFlag = deviceAppService.queryDeviceSwitch(authDeviceIsSupportAppVo.getDeviceInfo().getDeviceId());
        return ResultVO.ok(deviceFlag);
    }


    /**
     * 设置设备是否能够安装app的总开关
     *
     * @param imei
     * @param flag
     * @return
     */
    @ApiOperation(value = "设置设备是否能够安装app的总开", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imei", value = "设备imei号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "0关闭1打开", required = true, dataType = "int")
    })
    @PostMapping("/appInstallationSwitch")
    public ResultVO appInstallationSwitch(String imei, Integer flag) {
        logger.info("appInstallationSwitch deviceId={},flag={}", imei, flag);
        AuthDeviceIsSupportAppVo authDeviceIsSupportAppVo = deviceAppService.authDeviceIsSupportApp(imei);
        if (authDeviceIsSupportAppVo.getRespCode().getCode() != RespCode.SUCCESS.getCode()) {
            return ResultVO.error(authDeviceIsSupportAppVo.getRespCode());
        }
        deviceAppService.appInstallationSwitch(authDeviceIsSupportAppVo.getDeviceInfo(), flag);
        return ResultVO.ok();
    }

    /**
     * 设置单个app禁用
     *
     * @param setAppDisableDto
     * @return
     */
    @ApiOperation(value = "设置单个app禁用", notes = "")
    @PostMapping("/setAppDisable")
    public ResultVO setAppDisable(@RequestBody SetAppDisableDto setAppDisableDto) {
        logger.info("setAppDisable setAppDisableDto={}", setAppDisableDto.toString());
        AuthDeviceIsSupportAppVo authDeviceIsSupportAppVo = deviceAppService.authDeviceIsSupportApp(setAppDisableDto.getImei());
        if (authDeviceIsSupportAppVo.getRespCode().getCode() != RespCode.SUCCESS.getCode()) {
            return ResultVO.error(authDeviceIsSupportAppVo.getRespCode());
        }
        deviceAppService.setAppDisable(authDeviceIsSupportAppVo.getDeviceInfo(), setAppDisableDto.getPackageName(), setAppDisableDto.getAppFlag(), setAppDisableDto.getMqAppDisableTimes());
        return ResultVO.ok();
    }

    /**
     * 查询app禁用状态
     */
    @ApiOperation(value = "查询app禁用状态", notes = "")
    @GetMapping("/queryAppDisAbleState")
    public ResultVO<QueryAppDisAbleStateDto> queryAppDisAbleState(@RequestParam("imei") String imei, @RequestParam("packageName") String packageName) {
        logger.info("queryAppDisAbleState imei={}", imei);
        AuthDeviceIsSupportAppVo authDeviceIsSupportAppVo = deviceAppService.authDeviceIsSupportApp(imei);
        if (authDeviceIsSupportAppVo.getRespCode().getCode() != RespCode.SUCCESS.getCode()) {
            return ResultVO.error(authDeviceIsSupportAppVo.getRespCode());
        }
        return ResultVO.ok(deviceAppService.queryAppDisAbleState(authDeviceIsSupportAppVo.getDeviceInfo().getDeviceId(), packageName));
    }


}
